package assignment.quocthai;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AssignmentActivity extends Activity {
	public static List<MyAccountDTO> accountList;
	public static MyArrayAdapter myArrayAdapter;
	
	public MyAccountDAO myAccount;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myAccount = new MyAccountDAO(this);
        
        myAccount.insertAccount("Google", "bao", "123");
        myAccount.insertAccount("Yahoo!", "thien", "456");
        myAccount.insertAccount("Microsoft Live!", "thanh", "789");
        
        Cursor c = myAccount.getAllAccount();
        
        accountList = new ArrayList<MyAccountDTO>();

        ListView listView = (ListView) findViewById(R.id.listView1);        
        
        while(c.moveToNext()){
        	accountList.add(new MyAccountDTO(
        			c.getString(c.getColumnIndex(MyAccountDAO.ID_COL)), 
            		c.getString(c.getColumnIndex(MyAccountDAO.SERVER_NAME_COL)), 
            		c.getString(c.getColumnIndex(MyAccountDAO.USERNAME_COL)), 
            		c.getString(c.getColumnIndex(MyAccountDAO.PASSWORD_COL))));        	
        }
        c.close();
        
        myArrayAdapter = new MyArrayAdapter(this, R.layout.listview_row, accountList);
        
        listView.setAdapter(myArrayAdapter);
        
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				
				Log.i("id list view", String.valueOf(id));
				Log.i("position list view", String.valueOf(position));
				
				Intent formIntent = new Intent(AssignmentActivity.this, AddEditActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("action", "edit");
				bundle.putInt("position", position);
				formIntent.putExtras(bundle);
				startActivity(formIntent);
				
				return false;
			}
		});
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.layout.option_menu, menu);
    	
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		int id = item.getItemId();
		switch (id) {
		case R.id.item1://Add
			Intent formIntent = new Intent(AssignmentActivity.this, AddEditActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("action", "new");
			formIntent.putExtras(bundle);
			startActivity(formIntent);
			break;
		case R.id.item2://Delete
			AlertDialog.Builder ad = new AlertDialog.Builder(this);
			ad.setMessage("Enter the Account order that you want to delete(Start with 0)!");
			final EditText etDeletePosition = new EditText(this);
			ad.setView(etDeletePosition);
			ad.setCancelable(false);
			ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					if(!etDeletePosition.getText().toString().equals("")){
						int deletePosition = Integer.valueOf(etDeletePosition.getText().toString());

						myAccount.deleteAccount(myArrayAdapter.getItem(deletePosition).id);
						myArrayAdapter.remove(myArrayAdapter.getItem(deletePosition));
						myArrayAdapter.notifyDataSetChanged();
					}		
				}
			});						
			AlertDialog a = ad.create();
			a.show();
			break;
		case R.id.item3://Exit
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public class MyArrayAdapter extends ArrayAdapter<MyAccountDTO>{
		public List<MyAccountDTO> accountListArrayAdapter = new ArrayList<MyAccountDTO>();
		public MyArrayAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);
			// TODO Auto-generated constructor stub
		}

		public MyArrayAdapter(Context context, int textViewResourceId,
				List<MyAccountDTO> objects) {
			super(context, textViewResourceId, objects);
			accountListArrayAdapter = objects;
			// TODO Auto-generated constructor stub
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return super.getCount();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View v = convertView;
			if(v == null){
				LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);				
				v = inflater.inflate(R.layout.listview_row, null);
			}
			TextView tvServerName = (TextView) v.findViewById(R.id.tvServerNameRow);
//			tvServerName.setText(this.getItem(position).serverName);
			tvServerName.setText(accountListArrayAdapter.get(position).serverName);
			return v;
		}
		
	}
}